home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / pipes.zip / CLIENT.C < prev    next >
C/C++ Source or Header  |  1993-11-24  |  9KB  |  255 lines

  1. /********************************************************************\
  2. *  Julie Solon                                 *
  3. *  Microsoft Developer Support                                       *
  4. *  Copyright (c) 1992, 1993 Microsoft Corporation                    *
  5. *                                                                    *
  6. *  Comments:                                                         *
  7. *                                                                    *
  8. *  Functions:                                 *
  9. *                                                                    *
  10. *                                     *
  11. \********************************************************************/
  12.  
  13.  
  14. /*********************  Header Files  *********************/
  15.  
  16. #include <windows.h>
  17. #include <string.h>
  18. #include "client.h"
  19.  
  20.  
  21. /**********************  Defines  *************************/
  22.  
  23. #define BUFSIZE 512
  24.  
  25. /*********************  Prototypes  ***********************/
  26.  
  27. LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM, LPARAM );
  28. LRESULT WINAPI AboutDlgProc( HWND, UINT, WPARAM, LPARAM );
  29.  
  30. /*******************  Global Variables ********************/
  31.  
  32. HANDLE ghInstance;
  33.  
  34.  
  35. /********************************************************************\
  36. *  Function: int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int)    *
  37. *                                                                    *
  38. *   Purpose: Initializes Application                                 *
  39. *                                                                    *
  40. *  Comments: Standard template                                       *
  41. *                                                                    *
  42. *                                                                    *
  43. \********************************************************************/
  44.  
  45.  
  46. int PASCAL WinMain( HINSTANCE hInstance,
  47.             HINSTANCE hPrevInstance,
  48.             LPSTR lpszCmdLine,
  49.             int nCmdShow )
  50. {
  51.    WNDCLASS wc;
  52.    MSG msg;
  53.    HWND hWnd;
  54.  
  55.    if( !hPrevInstance ) {
  56.       wc.lpszClassName = "ClientClass";
  57.       wc.lpfnWndProc = MainWndProc;
  58.       wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
  59.       wc.hInstance = hInstance;
  60.       wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
  61.       wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  62.       wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 );
  63.       wc.lpszMenuName = "ClientMenu";
  64.       wc.cbClsExtra = 0;
  65.       wc.cbWndExtra = 0;
  66.  
  67.       RegisterClass( &wc );
  68.    }
  69.  
  70.    ghInstance = hInstance;
  71.  
  72.    hWnd = CreateWindow( "ClientClass",
  73.             "Client Application",
  74.                         WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL,
  75.                         0,
  76.                         0,
  77.                         CW_USEDEFAULT,
  78.                         CW_USEDEFAULT,
  79.                         NULL,
  80.                         NULL,
  81.                         hInstance,
  82.                         NULL
  83.                       );
  84.  
  85.    ShowWindow( hWnd, nCmdShow );
  86.  
  87.    while( GetMessage( &msg, NULL, 0, 0 ) ) {
  88.       TranslateMessage( &msg );
  89.       DispatchMessage( &msg );
  90.    }
  91.  
  92.    return msg.wParam;
  93. }
  94.  
  95.  
  96. /********************************************************************\
  97. * Function: LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM) *
  98. *                                                                    *
  99. *  Purpose: Processes Application Messages                           *
  100. *                                                                    *
  101. * Comments: The following messages are processed                     *
  102. *                                                                    *
  103. *           WM_CREATE                                                *
  104. *           WM_HSCROLL                                               *
  105. *           WM_VSCROLL                                               *
  106. *           WM_KEYDOWN                                               *
  107. *           WM_PAINT                                                 *
  108. *           WM_COMMAND                                               *
  109. *           WM_DESTROY                                               *
  110. *                                                                    *
  111. *                                                                    *
  112. \********************************************************************/
  113.  
  114.  
  115. LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam,
  116.    LPARAM lParam )
  117. {
  118.    PAINTSTRUCT ps;
  119.    HDC hDC;
  120.    HANDLE hPipe;
  121.    DWORD dwMode, dwBytes;
  122.    static char Buffer[BUFSIZE];
  123.    char ErrorBuf[80];
  124.  
  125.    switch( msg ) {
  126.  
  127. /**************************************************************\
  128. *     WM_CREATE:                           *
  129. \**************************************************************/
  130.  
  131.       case WM_CREATE:
  132.          strcpy( Buffer, "" );
  133.      break;
  134.  
  135. /**************************************************************\
  136. *     WM_PAINT:                            *
  137. \**************************************************************/
  138.  
  139.       case WM_PAINT:
  140.          hDC = BeginPaint( hWnd, &ps );
  141.  
  142.          TextOut( hDC, 10, 10, Buffer, strlen( Buffer ) );
  143.  
  144.      EndPaint( hWnd, &ps );
  145.          break;
  146.  
  147. /**************************************************************\
  148. *     WM_COMMAND:                           *
  149. \**************************************************************/
  150.  
  151.       case WM_COMMAND:
  152.          switch( wParam ) {
  153.         case IDM_CONNECT:
  154. #ifdef WIN32
  155.            hPipe = CreateFile( "//julieso/pipe/TestPipe",
  156.                           GENERIC_READ,
  157.                           FILE_SHARE_READ,
  158.                           NULL,
  159.                           OPEN_EXISTING,
  160.                           0,
  161.                           NULL
  162.                        );
  163.  
  164.                if( hPipe == INVALID_HANDLE_VALUE ) {
  165.                   wsprintf( ErrorBuf, "%d", GetLastError() );
  166.                   MessageBox( hWnd, ErrorBuf, "CreateFile()", MB_OK );
  167.                   break;
  168.                }
  169.                dwMode = PIPE_READMODE_MESSAGE;
  170.                SetNamedPipeHandleState( hPipe,
  171.                   &dwMode,
  172.                   NULL,
  173.                   NULL
  174.                );
  175.                ReadFile( hPipe,
  176.                   Buffer,
  177.                   BUFSIZE,
  178.                   &dwBytes,
  179.                   NULL
  180.            );
  181.            CloseHandle( hPipe );
  182.  
  183. #else
  184. {
  185.                OFSTRUCT Info;
  186.  
  187.            hPipe = OpenFile( "\\\\julieso\\pipe\\TestPipe",
  188.           &Info,
  189.           OF_READ | OF_SHARE_DENY_NONE
  190.            );
  191.            if( hPipe == HFILE_ERROR ) {
  192.           MessageBox( hWnd, "File not opened", "OpenFile()", MB_OK );
  193.           break;
  194.            }
  195.            _lread( hPipe, Buffer, BUFSIZE );
  196.                _lclose( hPipe );
  197. }
  198. #endif
  199.            InvalidateRect( hWnd, NULL, TRUE );
  200.                break;
  201.  
  202.         case IDM_ABOUT:
  203.                DialogBox( ghInstance, "AboutDlg", hWnd, (DLGPROC)  
  204.                              AboutDlgProc );
  205.                break;
  206.          }
  207.          break;
  208.  
  209. /**************************************************************\
  210. *     WM_DESTROY: PostQuitMessage() is called                  *
  211. \**************************************************************/
  212.  
  213.       case WM_DESTROY:
  214.          PostQuitMessage( 0 );
  215.          break;
  216.  
  217. /**************************************************************\
  218. *     Let the default window proc handle all other messages    *
  219. \**************************************************************/
  220.  
  221.       default:
  222.          return( DefWindowProc( hWnd, msg, wParam, lParam ));
  223.    }
  224.  
  225.    return 0;
  226. }
  227.  
  228. /********************************************************************\
  229. * Function: LRESULT CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM)*
  230. *                                                                    *
  231. *  Purpose: Processes "About" Dialog Box Messages                    *
  232. *                                                                    *
  233. * Comments: The Dialog Box is displayed when the user selects        *
  234. *           Help.About.                                              *
  235. *                                                                    *
  236. \********************************************************************/
  237.  
  238.  
  239. LRESULT CALLBACK AboutDlgProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
  240. {
  241.    switch( uMsg ) {
  242.       case WM_INITDIALOG:
  243.          return TRUE;
  244.       case WM_COMMAND:
  245.          switch( wParam ) {
  246.             case IDOK:
  247.                EndDialog( hDlg, TRUE );
  248.                return TRUE;
  249.          }
  250.       break;
  251.    }
  252.  
  253.    return FALSE;
  254. }
  255.